import java.util.HashMap;
import java.util.Map;

public class Solution2179 {
    static class BIT{

        int[] h;
        int n;

        public BIT(int n){
            h=new int[n+1];
            this.n=n;
        }

        private int lowBit(int x){
            return x & (-x);
        }

        int query(int x){
            int t=0;
            while (x!=0){
                t=t+h[x];
                x-=lowBit(x);
            }
            return t;
        }

        void update(int x,int t){
            while (x<=n){
                h[x]=h[x]+t;
                x+=lowBit(x);
            }
        }
    }

    public long goodTriplets(int[] nums1, int[] nums2) {
        Map<Integer,Integer> map=new HashMap<>();
        for (int i=0;i<nums1.length;i++){
            map.put(nums1[i],i);
        }
        int[] target=new int[nums1.length];
        for (int i=0;i<nums1.length;i++){
            target[i]=map.get(nums2[i])+1;
        }
        BIT bit1=new BIT(nums1.length);
        for (int i=0;i<target.length;i++){
            bit1.update(nums1.length-target[i]+1,1);
        }
        long count=0;
        BIT bit2=new BIT(nums1.length);
        for (int i=0;i<target.length;i++){
            bit1.update(nums1.length-target[i]+1,-1);
            count=count+(long)bit1.query(nums1.length-target[i]+1)*bit2.query(target[i]);
            bit2.update(target[i],1);
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(new Solution2179().goodTriplets(new int[]{13,14,10,2,12,3,9,11,15,8,4,7,0,6,5,1},new int[]{8,7,9,5,6,14,15,10,2,11,4,13,3,12,1,0}));
    }
}
